package com.axx.android.profile.auth.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.axx.android.LetterApp;
import com.axx.android.R;
import com.axx.android.base.BaseActivity;
import com.axx.android.common.BaseModel;
import com.axx.android.common.Constants;
import com.axx.android.profile.auth.model.AuthUser;
import com.axx.android.setting.ModifyPassActivity;
import com.axx.android.utils.ContextUtils;
import com.axx.android.utils.GsonUtils;
import com.axx.android.utils.HttpUtils;
import com.axx.android.utils.MapUtils;
import com.google.gson.reflect.TypeToken;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.Tag;

import org.json.JSONException;

import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends BaseActivity {

    @Bind(R.id.btn_back)
    Button btnBack;
    @Bind(R.id.et_password)
    EditText etPassword;
    @Bind(R.id.btn_login)
    Button btnLogin;
    @Bind(R.id.et_username)
    EditText etUsername;
    @Bind(R.id.btn_reg)
    Button mBtnReg;


    @Override
    public int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void init(Bundle savedInstanceState) {

    }

    private void login(String username, final String password) {
        Map<String, String> map = new HashMap<>();
        map.put("phone", username);
        map.put("psw", password);
        map.put("loginType", "1");
        map.put("loginLongitude", LetterApp.curLocation.getLongitude() + "");
        map.put("loginLatitude", LetterApp.curLocation.getLatitude() + "");
        map.put("cityCode", MapUtils.getCity(LetterApp.curLocation.getAdCode()));
        map.put("cityName", LetterApp.curLocation.getCity());


        HttpUtils.post(Constants.Url.User.USER_LOGIN, map, new HttpUtils.SJCallBack() {
            @Override
            public void onSuccess(String result) {
                hideLoading();
                try {
                    BaseModel<AuthUser> authUserBaseModel = GsonUtils.fromJson(result, new TypeToken<BaseModel<AuthUser>>() {
                    });
                    if (authUserBaseModel.isSuccess()) {
                        finish();
                        LetterApp.authUser = authUserBaseModel.data;
                        LetterApp.authUser.setToken(authUserBaseModel.token);
                        LetterApp.authUser.setPassword(password);

                        //初始化推送
                        PushManager.getInstance().bindAlias(LoginActivity.this,authUserBaseModel.data.getId());
                        PushManager.getInstance().turnOnPush(LoginActivity.this);

                        ContextUtils.saveUser(LoginActivity.this,LetterApp.authUser);

                    } else {
                        showToast(authUserBaseModel.message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                    showErrorParse();
                }
            }

            @Override
            public void onError() {
                hideLoading();
                showErrorNet();
            }
        });

    }


    @OnClick({R.id.btn_reg, R.id.btn_login,R.id.tv_forget_pwd})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_reg:
                reg();
                break;
            case R.id.btn_login:
                login();
                break;
            case R.id.tv_forget_pwd:
               Intent intent =  new Intent(this,ModifyPassActivity.class);
                intent.putExtra("title","忘记密码");
                startActivity(intent);
                break;
        }
    }

    private void reg() {
        startActivity(new Intent(this, RegActivity.class));
    }

    private void login() {
        if (TextUtils.isEmpty(etUsername.getText().toString())) {
            showToast("用户名不能为空");
            return;
        }
        if (TextUtils.isEmpty(etPassword.getText().toString())) {
            showToast("密码不能为空");
            return;
        }

        showLoading("登录中");
        login(etUsername.getText().toString(), etPassword.getText().toString());
    }
}
